package Utils;

# part of ostry's LANmgmt, request newest version ostrochovsky@rec.uniba.sk

use strict;
use Data::Dumper;

use NetAddr::IP;

sub get_own_fqdn {
	my $hostname = `hostname`;
	my $dnsname = `dnsdomainname`;
	chomp($hostname);
	chomp($dnsname);
	return $hostname." (".$dnsname.")";
}

sub generation_mark {
	my $file = shift;			# pouzitie: Utils::generation_mark(__FILE__);
	return "vygenerovane ".Utils::current_time_slovak()." systemom LANmgmt ".$file." @ ".get_own_fqdn;
}

sub number2bytes {
	my $number = shift;
	
	my $numberK = $number/1000;
	if ($numberK > 1) {
		$number = round($numberK)."K";
		my $numberM = $numberK/1000;
		if ($numberM > 1) {
			$number = round($numberM)."M";
		}	
	}
	return $number;
}

sub round {
    my($number) = shift;
    return int($number + .5);
}

sub spaces {
	my $count = shift;
	my $spaces;
	for (my $i = 1; $i <= $count; $i++) {
		$spaces .= ' ';
	}
	return $spaces;
}

sub string2ASCIIarray {
        my $string = shift;
        return unpack("C*",$string);    # array of ASCII values
}

sub current_timestamp {
	my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time);
	$year += 1900;
	$min = addZeros($min, 2);
	$sec = addZeros($sec, 2);
	$mon += 1;
	return "${mday}.${mon}.${year}_${hour}-${min}-${sec}_UTC";
}

sub current_time_slovak {
	my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = gmtime(time);
	$year += 1900;
	$min = addZeros($min, 2);
	$sec = addZeros($sec, 2);
	$mon += 1;
	return "${mday}.${mon}.${year} ${hour}:${min}:${sec} (UTC)";
}

sub MAChex2dec {
        my $MAC = shift;
        my @MAC = split(':',$MAC);
        for (my $i=0; $i<6; $i++) {
                $MAC[$i] = hex($MAC[$i]);
        }
        return join ".",@MAC;
}

sub MACdec2hex {
        my $MAC = shift;
        my @MAC = split(/\./,$MAC);
        for (my $i=0; $i < 6; $i++) {
                $MAC[$i] = sprintf("%x", $MAC[$i]);
                $MAC[$i] = addZeros($MAC[$i],2);
        }
        return join ":",@MAC;
}

sub MACshort2normal {	# niektore arpwatchy neuvadzaju nuly na zaciatku jednotlivych bajtov, takze to treba napravit
	my $MAC = shift;
	my @MAC = split ':', $MAC;
	my $i = 0;
	foreach my $byte (@MAC) {
		$MAC[$i] = addZeros($MAC[$i],2);
		$i++;
	}
	return join ':',@MAC;
}

sub MACcolon2minus {
	my $MAC = shift;
	my @MAC = split /\:/, $MAC;
	return join '-', @MAC;
}

sub addZeros {	# zarovna nulami na dlzku length
        my ($string,$length) = @_;
	my $dif = $length - length($string);
        if ($dif > 0) {
		for (my $i=1; $i<=$dif; $i++) {
			$string = "0".$string;
		}
	}	
	return $string;
}

sub is_in_array {
	my $element = shift;
	my @array = shift;
#	print "array: ".Dumper @array;
	if (!defined $array[0]) { return 0; }
	foreach my $elem (@array) {
#		print "elem: ".Dumper $elem;
#		print "element: ".Dumper $element;
		if ($elem eq $element) {
			return 1;
		}
	}
	return 0;
}

sub isIPvalid {
	my $IP = shift;
	if ($IP =~ /^[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}$/) {
	        my @IP = split(/\./, $ARGV[0]);
        	foreach my $num (@IP) {
                	if ($num >= 256) {
                        	return 0;	# at least one of 4 decimal numbers is too high
                	}
        	}
		return 1;	# IP address is OK
	} else {
		return 0;	# bad form
	}
#        if (!$incorrect_form) {
#                $input_is_mac = 0;
#                $segment = Network->IP2Segment($ARGV[0]);
#                if (!$segment) {
#                        die "IP address $ARGV[0] is out of our subnet spaces\n";
#                }
#        }
}

sub isMACvalid {
	my $MAC = shift;
	if ($MAC =~ /^[0-9,a-f,A-F]{2}\:{1}[0-9,a-f,A-F]{2}\:{1}[0-9,a-f,A-F]{2}\:{1}[0-9,a-f,A-F]{2}\:{1}[0-9,a-f,A-F]{2}\:{1}[0-9,a-f,A-F]{2}$/) {
		return 1;	# MAC is valid
	} else {
		return 0;	# incorrect form
	}
}

sub ethernet_max_frame_size {
	return 1518;
}

sub ethernet_min_frame_size {
	return 64;
}

1;

